import sys
import traceback

from xjlibrary.all_step.BaseAllStep import GlobalControl
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
print(curPath)
sys.path.insert(0, curPath)  # 插入

from Step1_getSpecialistSubject import main as step1_main
from Step2_getJournalName import main as step2_main
from Step3_getVolumeByJournal import main as step3_main
from Step4_getIssueiByVolume import main as step4_main
from Step5_getAricleByIssue import main as step5_main
from Step6_downloadArticleDetail import main as step6_main
from Step7_downloadCover import main as step7_main
"""


from Step5_parser_issue import main as step5_main
from Step6_down_archive import main as step6_main
from Step7_parser_archive import main as step7_main
from Step8_down_cover import main as step8_main
from Step9_map_cover import main as step9_main
"""



class UseGlobalControl(GlobalControl):

    def __init__(self):
        super().__init__(__file__, BaseDir.get_new_path(BaseDir.get_file_dir_absolute(__file__), "logs"))

    def imports(self):
        # import_module("Step1_save_url_periodical.main")
        # import_module("Step2_save_time_list.main")
        # import_module("Step3_parse_timedetails.main")
        # import_module("Step4_save_issue_list.main")
        # import_module("Step5_parse_issue_fulltext.main")
        # import_module("Step6_save_fulltext_list.main")
        # import_module("Step7_parse_fulltext_list.main")
        # import_module("Step8_Update_cover.main")
        # import_module("Step9_down_cover.main")
        pass

    def init(self):
        sql = "update journal set cover_stat = 0 where cover_stat=1"
        self.mysqlutils.ExeSqlToDB(sql)
        super().init()

    def run(self, strings):
        """
        该步骤运行代码
        :return:
        """
        try:
            if strings == "":
                return True
            result = eval(strings)
        except:
            print(traceback.format_exc())
            return False
        return result

    def start_hook(self, key):
        if key == "8":
            self.key = "8"
            self.nowstep = ""
            return True

    def one_check(self):
        """
        该步骤检查代码是否运行完成
        :return:
        """
        if self.key == "1":
            print("检查第一步")
            sql = "select stat from subject where stat=0"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 30:
                print("执行正常")
                self.writetrue()
            else:
                self.writefalse("量不对")
        elif self.key == "2":
            print("检查第二步")

            sql = "select stat from subject where stat=0"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 0:
                print("执行正常")
                self.writetrue()
            else:
                self.writefalse("量不对")

        elif self.key == "3":

            sql = "select stat from journal where stat=0 and failcount<3"
            rows = self.mysqlutils.SelectFromDB(sql)
            if not rows:
                self.writetrue()
            else:
                self.writefalse("")
        elif self.key == "4":
            sql = "SELECT vol_url,jid,jname FROM volume WHERE stat = 0 AND failcount < 5"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 0:
                self.writetrue()
            else:
                self.writefalse("有ams_volume没有下载 请检查原因")
        elif self.key == "5":
            sql = "SELECT issue_url,jsonmsg FROM issue WHERE stat = -1 AND failcount < 5 limit 1000"
            rows = self.mysqlutils.SelectFromDB(sql)
            if not rows:
                self.writetrue()
            else:
                self.writefalse("有issue没有下载 请检查原因")
        elif self.key == "6":
            sql = "SELECT `doi`,`jsonmsg` FROM article WHERE stat=0 AND failcount<4 limit 300;"
            rows = self.mysqlutils.SelectFromDB(sql)
            if not rows:
                self.writetrue()
            else:
                self.writefalse("article表中有状态为0的数据表示没有下载完")
        elif self.key == "7":
            sql = "SELECT `jid`, `name` FROM journal WHERE cover_stat = 0"
            rows = self.mysqlutils.SelectFromDB(sql)
            if not rows:
                self.writetrue()
            else:
                self.writefalse("还有图片没有下载完")
        elif self.key == "8":
            sql = "SELECT `cover_url`,`url` FROM `journal` WHERE `stat` != 2 and cover_url != ''"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 37:
                self.writetrue()
            else:
                self.writefalse("图片数量不是37")
        elif self.key == "9":
            from Step9_down_cover import sPath
            if BaseDir.get_dir_file_num(sPath) == 37:
                self.writetrue()
            else:
                self.writefalse("图片数量不是37")

        if "9" in self.jsondicts.keys() and self.jsondicts["9"]["isFinish"]:
            return True
        return False


if __name__ == "__main__":
    globalcontrol = UseGlobalControl()
    globalcontrol.work()
